# coding: utf-8

# -------------------------------------------------------------------------------
# Name:         http_session_context.py
# Description:
# Author:       XiangjunZhao
# EMAIL:        2419352654@qq.com
# Date:         2019/11/18 17:32
# -------------------------------------------------------------------------------

from apps.HttpAutoTestService.core import utils


class HttpSessionContext(object):
    """会话上下文，用在存储运行时变量"""

    def __init__(self, environment_variables=None, project_variables=None, testsuite_variables=None):
        self.session_variables_mapping = {}
        self.output_variables_mapping = {}

        environment_variables = utils.ensure_mapping_format(environment_variables or {})
        project_variables = utils.ensure_mapping_format(project_variables or {})
        testsuite_variables = utils.ensure_mapping_format(testsuite_variables or {})

        self.update_session_variables(environment_variables)
        self.update_session_variables(project_variables)
        self.update_session_variables(testsuite_variables)

    def update_session_variables(self, variables_mapping):
        """
        更新HttpSessionContext实例的变量
        :param variables_mapping:变量参数
        """
        variables_mapping = utils.ensure_mapping_format(variables_mapping)
        if variables_mapping:
            self.session_variables_mapping.update(variables_mapping)

    def update_output_variables(self, variables_mapping):
        variables_mapping = utils.ensure_mapping_format(variables_mapping)
        if variables_mapping:
            self.output_variables_mapping.update(variables_mapping)
